let config = require('./config');

(function(exports) {
   //更新基本信息
   var apis = {
      getActivity:{
         method: "GET",
        url: "/activity_material/",
      },
      saveParticipantInfo: {
         method: "POST",
         url: "/participant_information/",
      },
   }
   for (var key in apis) {
      exports[key] = {
         header: {
            'content-type': 'application/json'
         },
         url: config.domain + apis[key].url,
         method: apis[key].method,
         placeholder: apis[key].placeholder,
      }
   }
   //执行微信请求
   exports.WxRequest = function(params, data, callback) {
      if (params.placeholder) {
         params.placeholder.forEach(item => {
            params.url = params.url.replace(item, data[item.slice('1')]);
         });
      }

      wx.request({
         url: params.url,
         header: params.header,
         method: params.method == "POST" ? "POST" : "GET",
         data: Object.assign({}, data),
         success: function(res) {
            if (res.statusCode == 200) {
               callback(res.data)
            } else {
               throw new Error('Request "' + params.url + '" failed');
            }
         }
      })
   }

}(module.exports))